package com.acwing.partition36;

import java.io.*;
import java.util.Arrays;

/**
 * @author rkc
 * @date 2022/7/24 9:31
 */
public class AC3555二叉树 {

    private static final int N = 1010, M = N, D = (int) (Math.log(N) / Math.log(2));
    private static int[] h = new int[N], e = new int[M], ne = new int[M], dep = new int[N];
    private static int[] q = new int[N], dist = new int[N];
    private static int[][] f = new int[N][D + 1];
    private static int n, m, idx = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int k = nextInt();
        while (k-- > 0) {
            n = nextInt(); m = nextInt();
            reset();
            for (int i = 1; i <= n; i++) {
                int l = nextInt(), r = nextInt();
                if (l != -1) add(i, l);
                if (r != -1) add(i, r);
            }
            bfs(1);
            for (int i = 0; i < m; i++) {
                int u = nextInt(), v = nextInt(), anc = lca(u, v);
                writer.println(dist[u] + dist[v] - 2 * dist[anc]);
            }
        }
        writer.flush();
    }

    private static int lca(int x, int y) {
        if (dep[x] < dep[y]) {
            int t = x;
            x = y;
            y = t;
        }
        for (int i = D; i >= 0; i--) {
            if (dep[f[x][i]] >= dep[y]) {
                x = f[x][i];
            }
        }
        if (x == y) return x;
        for (int i = D; i >= 0; i--) {
            if (f[x][i] != f[y][i]) {
                x = f[x][i];
                y = f[y][i];
            }
        }
        return f[x][0];
    }

    private static void bfs(int root) {
        int qh = 0, qt = -1;
        q[++qt] = root;
        dep[root] = 1;
        while (qh <= qt) {
            int u = q[qh++];
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                if (dep[v] != 0) continue;
                dep[v] = dep[u] + 1;
                dist[v] = dist[u] + 1;
                f[v][0] = u;
                for (int j = 1; j <= D; j++) {
                    int anc = f[v][j - 1];
                    f[v][j] = f[anc][j - 1];
                }
                q[++qt] = v;
            }
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    private static void reset() {
        idx = 0;
        for (int i = 0; i < N; i++) {
            Arrays.fill(f[i], 0);
            dist[i] = dep[i] = 0;
            h[i] = -1;
        }
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
